package com.biz.primus.ms.product.service;

import com.biz.primus.base.exception.utils.AssertUtils;
import com.biz.primus.base.vo.commodity.ProductPriceVo;
import com.biz.primus.commodity.exception.CommodityExceptionType;
import com.biz.primus.model.price.vo.BatchPriceVo;
import com.biz.primus.model.price.vo.PriceReqVo;
import com.biz.primus.ms.product.feign.PriceFeignClient;
import com.biz.primus.ms.product.interfaces.ProductPriceService;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author jiangnan
 * @Description: TODO
 * @create 2017-10-12-下午3:50
 */
@Slf4j
@Service
public class ProductPriceServiceImpl implements ProductPriceService {

    @Autowired
    private PriceFeignClient priceFeignClient;

    @Override
    public List<ProductPriceVo> getPrices(PriceReqVo reqVo) {
        log.debug("getPrices reqVo:{}", reqVo);
        log.debug("priceFeignClient.price reqVo:{}", reqVo);
        BatchPriceVo batchPriceVo = priceFeignClient.prices(reqVo);
        log.debug("priceFeignClient.price respVo:{}", batchPriceVo);
        AssertUtils.isTrue((batchPriceVo.getDepotPrices().size() == batchPriceVo.getVirtualPrices().size()) &&
                        (reqVo.getProductCodes().size() == batchPriceVo.getDepotPrices().size()),
                CommodityExceptionType.COMMON_EXCEPTION, "价格数据异常");
        List<ProductPriceVo> respVos = Lists.newArrayList();
        for (int i = 0; i < reqVo.getProductCodes().size(); i++) {
            respVos.add(new ProductPriceVo(batchPriceVo.getDepotPrices().get(i),batchPriceVo.getVirtualPrices().get(i)));
        }
        log.debug("getPrices respVo:{}",respVos);
        return respVos;
    }


}
